﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Using

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using paravili.Services;
using Sebarf.Core.Interfaces;

#endregion

namespace paravili.Ui {
	/// <summary>
	/// Interaction logi5c for ActivityRatingVisualizer.xaml
	/// </summary>
	public partial class ActivityRatingVisualizer : UserControl {
		#region Public Methods

		public ActivityRatingVisualizer() {
			InitializeComponent();
		}

		public void Init(IServiceLocatorService serviceLocator) {
			m_visualizer.Visibility = Visibility.Hidden;
			serviceLocator.GetRegisteredService<IStatisticValuesLocatorService>().StatisticValueEntryAdded +=
				StatService_StatisticValueEntryAdded;
			serviceLocator.GetRegisteredService<IResetHandlerService>().Reset += OnReset;
		}

		#endregion

		#region Private Methods

		private void OnReset(object sender, EventArgs e) {
			m_visualizer.Visibility = Visibility.Hidden;
		}

		private void StatService_StatisticValueEntryAdded(object sender, StatisticValuesLocatorServiceEventArgs e) {
			if (e.Entry.Name == "Movement@ActivityRating") {
				m_rating = e.Entry;
				m_rating.PropertyChanged += OnValueChanged;
			}
			if (e.Entry.Name == "Movement@ClusterAreaCoverageRatio") {
				m_coverageRatio = e.Entry;
				m_coverageRatio.PropertyChanged += OnValueChanged;
			}
		}

		private void OnValueChanged(object sender, PropertyChangedEventArgs e) {
			if (m_rating != null && m_coverageRatio != null) {
				m_visualizer.Visibility = Visibility.Visible;
				var rating = (double)m_rating.Value;
				var coverageRatio = (double)m_coverageRatio.Value;
				var value = (rating + coverageRatio) / 2.0;
				m_visualizer.Margin = new Thickness(2, Math.Max(0, (1.0 - Math.Min(1, value))) * ActualHeight, 2, 2);

			}
		}

		#endregion

		#region Private Fields

		private StatisticValueEntry m_coverageRatio;
		private double m_maxRating;
		private StatisticValueEntry m_rating;

		#endregion
	}
}